﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hMiniMap: hVisibleGameObject
    {
        Texture2D txtSign;
        Texture2D txtFog;
        List<Vector2> lstGreenSign;
        List<Vector2> lstBlueSign;
        List<Vector2> lstMagentaSign;
        List<Vector2> lstRedSign;
        List<Vector2> lstYellowSign;
        List<Vector2> lstFog;
        float scaleX;
        float scaleY;
        

        public hMiniMap(ContentManager content, hParams args)
        {
            txtSign = content.Load<Texture2D>(hGlobal.PathGui + "MiniMapSign");
            txtFog = content.Load<Texture2D>(hGlobal.PathTerrain + "Fog"); 
            
            lstGreenSign = new List<Vector2>();
            lstBlueSign = new List<Vector2>();
            lstRedSign = new List<Vector2>();
            lstYellowSign = new List<Vector2>();
            lstFog = new List<Vector2>();
            lstMagentaSign = new List<Vector2>();

            string[] s = new string[1];
            s[0]= hGlobal.PathGui + "MiniMapBackGround";
            create1SpriteObject(content,s);

            // kích thước chuẩn
            X = 10;
            Y = 455;
            Width = 140;
            Height = 140;

            // co kéo cho phù hợp
            float W = args.MapManager.Map.Width;
            float H = args.MapManager.Map.Height;

            if (W != H)
            {
                if (W < H)
                {
                    Width = (Height / H) * W;
                }
                else
                {
                    Height = (Width / W) * H;
                }
            }

            X = X + (140 / 2) - Width / 2;
            Y = Y + (140 / 2) - Height / 2;
            scaleX = args.MapManager.Map.Width / Width;
            scaleY = args.MapManager.Map.Height / Height;
        }

        public override void update(hParams args, Microsoft.Xna.Framework.GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();
            if (mouse.X > X && mouse.X < X + Width
                && mouse.Y > Y && mouse.Y < Y + Height
                && mouse.LeftButton==ButtonState.Pressed)
            {
                float iX = mouse.X - X;
                float iY = mouse.Y - Y;
                float realX = iX * scaleX;
                float realY = iY *scaleY;

                args.Grid.nextX= -(realX - hGlobal.ViewWidth / 2);
                args.Grid.nextY = -(realY - hGlobal.ViewHeight / 2);

                if (args.Grid.nextX < hGlobal.ViewWidth - args.Grid.Width)
                {
                    args.Grid.nextX = hGlobal.ViewWidth - args.Grid.Width;
                }

                if (args.Grid.nextX > 0)
                {
                    args.Grid.nextX = 0;
                }

                if (args.Grid.nextY < hGlobal.ViewHeight - args.Grid.Height)
                {
                    args.Grid.nextY = hGlobal.ViewHeight - args.Grid.Height;
                }

                if (args.Grid.nextY > 0)
                {
                    args.Grid.nextY = 0;
                }

            }

            lstGreenSign.Clear();
            lstRedSign.Clear();
            lstYellowSign.Clear();
            lstFog.Clear();
            lstBlueSign.Clear();
            lstMagentaSign.Clear();

            for (int i = 0; i < args.StructureManager.NRows;i++ )
                for (int j = 0; j < args.StructureManager.NColumns; j++)
                {
                    if (args.StructureManager.Structures[i, j] != null && args.MapManager.Map.MapCells[i,j].onFogOfWar==false)
                    {
                        switch(args.StructureManager.Structures[i, j].Controler)
                        {
                            case "player":
                                lstGreenSign.Add(new Vector2(X + j * hGlobal.MapCellWidth / scaleX, Y + i * hGlobal.MapCellHeight / scaleY));
                                break;
                            case "computer":
                                lstRedSign.Add(new Vector2(X + j * hGlobal.MapCellWidth / scaleX, Y + i * hGlobal.MapCellHeight / scaleY));
                                break;
                            default:
                                lstYellowSign.Add(new Vector2(X + j * hGlobal.MapCellWidth / scaleX, Y + i * hGlobal.MapCellHeight / scaleY));
                                break;
                        }
                    }
                    if (args.MapManager.Map.MapCells[i, j].onFogOfWar)
                    {
                        lstFog.Add(new Vector2(X + j * hGlobal.MapCellWidth / scaleX, Y + i * hGlobal.MapCellHeight / scaleY));
                    }
                }

            for (int i = 0; i < args.UnitManager.Units.Count; i++)
            {
                switch (args.UnitManager.Units[i].Controller)
                {
                    case "player":
                        lstBlueSign.Add(new Vector2(X + (args.UnitManager.Units[i].X - args.MapManager.Map.X) / scaleX, Y + (args.UnitManager.Units[i].Y - args.MapManager.Map.Y )/ scaleY));
                        break;
                    case "computer":
                        lstMagentaSign.Add(new Vector2(X + (args.UnitManager.Units[i].X - args.MapManager.Map.X) / scaleX, Y + (args.UnitManager.Units[i].Y - args.MapManager.Map.Y) / scaleY));
                        break;
                    default:
                        lstYellowSign.Add(new Vector2(X + (args.UnitManager.Units[i].X - args.MapManager.Map.X) / scaleX, Y + (args.UnitManager.Units[i].Y - args.MapManager.Map.Y) / scaleY));
                        break;
                }
            }

            base.update(args, gameTime);
        }

        public void draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_sprite[0].Texture[0], new Rectangle((int)X, (int)Y, (int)Width, (int)Height), Color.White);
            for (int i = 0; i < lstFog.Count; i++)
            {
                spriteBatch.Draw(txtFog, new Rectangle((int)(lstFog[i].X), (int)(lstFog[i].Y), (int)(txtFog.Width / scaleX), (int)(txtFog.Height / scaleY)), Color.White);
            }
            for (int i = 0; i < lstGreenSign.Count; i++)
            {
                spriteBatch.Draw(txtSign, lstGreenSign[i], Color.Green);
            }
            for (int i = 0; i < lstRedSign.Count; i++)
            {
                spriteBatch.Draw(txtSign, lstRedSign[i], Color.Red);
            }
            for (int i = 0; i < lstYellowSign.Count; i++)
            {
                spriteBatch.Draw(txtSign, lstYellowSign[i], Color.Yellow);
            }
            for (int i = 0; i < lstBlueSign.Count; i++)
            {
                spriteBatch.Draw(txtSign, lstBlueSign[i], Color.Blue);
            }
            for (int i = 0; i < lstMagentaSign.Count; i++)
            {
                spriteBatch.Draw(txtSign, lstMagentaSign[i], Color.Magenta);
            }  
        }
    }
}
